package query

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"k9-panel/config"
	"log"
	"os"
	"time"
)

func Init(cfg *config.Configuration) *gorm.DB {
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // 输出到标准输出
		logger.Config{
			SlowThreshold:             time.Second, // 慢查询阈值
			LogLevel:                  logger.Info, // 设置日志级别为 Info
			IgnoreRecordNotFoundError: true,        // 忽略未找到记录的错误
			ParameterizedQueries:      true,        // 打印参数化查询
			Colorful:                  false,       // 禁用彩色日志
		},
	)

	dsn := cfg.DSN
	gDB, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: newLogger,
	})
	project1 := newProject(gDB)
	Project = &project1
	environment1 := newEnvironment(gDB)
	Environment = &environment1

	baseConfig1 := newBaseConfig(gDB)
	BaseConfig = &baseConfig1

	derivedConfig1 := newDerivedConfig(gDB)
	DerivedConfig = &derivedConfig1

	historyConfig1 := newHistoryConfig(gDB)
	HistoryConfig = &historyConfig1

	projectEnvironment1 := newProjectEnvironment(gDB)
	ProjectEnvironment = &projectEnvironment1

	token1 := newUserToken(gDB)
	UserToken = &token1

	account1 := newAccount(gDB)
	Account = &account1
	return gDB
}
